| Conditions | 12 |
| Paths | 6147 |
| Total Lines | 157 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like get-main.js ➔ route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import xss from 'xss' |
||
| 19 | var route = function(req, res, next) { |
||
| 20 | if(req.query.filePath){ |
||
| 21 | var testXSS = xss(req.query.filePath, { |
||
| 22 | whiteList: [], |
||
| 23 | stripIgnoreTag: true |
||
| 24 | }) |
||
| 25 | if(testXSS !== req.query.filePath){ |
||
| 26 | res.redirect(`/abe/${req.params[0]}?filePath=${testXSS}`) |
||
| 27 | return |
||
| 28 | } |
||
| 29 | } |
||
| 30 | Hooks.instance.trigger('beforeRoute', req, res, next) |
||
| 31 | if(typeof res._header !== 'undefined' && res._header !== null) return |
||
|
|
|||
| 32 | |||
| 33 | var templatePath = req.params[0] |
||
| 34 | |||
| 35 | var filePath = null |
||
| 36 | if(typeof req.query.filePath !== 'undefined' && req.query.filePath !== null) { |
||
| 37 | filePath = path.join(config.root, config.draft.url, req.query.filePath.replace(config.root)) |
||
| 38 | } |
||
| 39 | var debugJson = (req.query.debugJson && req.query.debugJson == 'true' ) ? true : false |
||
| 40 | var debugJsonKey = (req.query.key) ? req.query.key : false |
||
| 41 | var debugHtml = (req.query.debugHtml && req.query.debugHtml == 'true' ) ? true : false |
||
| 42 | |||
| 43 | var isHome = true |
||
| 44 | |||
| 45 | let p = new Promise((resolve) => { |
||
| 46 | |||
| 47 | if(templatePath !== null && filePath !== null) { |
||
| 48 | var jsonPath = null |
||
| 49 | var linkPath = null |
||
| 50 | isHome = false |
||
| 51 | |||
| 52 | var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath) |
||
| 53 | if(typeof filePathTest !== 'undefined' && filePathTest !== null) { |
||
| 54 | // filePath = filePathTest.path |
||
| 55 | jsonPath = filePathTest.path |
||
| 56 | linkPath = filePathTest.abe_meta.link |
||
| 57 | } |
||
| 58 | |||
| 59 | if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { |
||
| 60 | res.redirect('/abe/') |
||
| 61 | return |
||
| 62 | } |
||
| 63 | |||
| 64 | editor(templatePath, jsonPath, linkPath) |
||
| 65 | .then((result) => { |
||
| 66 | var manager = {} |
||
| 67 | var revisionFilePath = FileParser.changePathEnv(filePath, config.draft.url) |
||
| 68 | var dirPath = path.dirname(revisionFilePath) |
||
| 69 | var allDraft = FileParser.getFiles(dirPath, true, 99, new RegExp('\\.' + config.files.templates.extension)) |
||
| 70 | |||
| 71 | allDraft = cmsData.metas.get(allDraft, 'draft') |
||
| 72 | var breadcrumb = req.params[0].split('/') |
||
| 73 | manager.file = { |
||
| 74 | revision: cmsData.revision.getFilesRevision(allDraft, cmsData.fileAttr.delete(revisionFilePath)) |
||
| 75 | ,template: breadcrumb |
||
| 76 | ,path: (req.query.filePath) ? path.resolve(req.query.filePath).replace(/^\//, '') : '' |
||
| 77 | } |
||
| 78 | if(manager.file.revision.length > 0){ |
||
| 79 | var publishPath = cmsData.fileAttr.delete(manager.file.revision[0].path.replace(new RegExp(`/${config.draft.url}/`), `/${config.publish.url}/`)) |
||
| 80 | manager.file.isPublished = coreUtils.file.exist(publishPath) |
||
| 81 | } |
||
| 82 | |||
| 83 | resolve({ |
||
| 84 | obj: result, |
||
| 85 | manager: manager |
||
| 86 | }) |
||
| 87 | }).catch(function(e) { |
||
| 88 | console.error(e) |
||
| 89 | }) |
||
| 90 | }else { |
||
| 91 | resolve({ |
||
| 92 | obj: {}, |
||
| 93 | manager: {} |
||
| 94 | }) |
||
| 95 | } |
||
| 96 | }).catch(function(e) { |
||
| 97 | console.error(e) // "oh, no!" |
||
| 98 | }) |
||
| 99 | |||
| 100 | p.then((result) => { |
||
| 101 | var obj = result.obj |
||
| 102 | var manager = result.manager |
||
| 103 | // let tplUrl = result.tplUrl |
||
| 104 | |||
| 105 | manager.home = { |
||
| 106 | files: Manager.instance.getList() |
||
| 107 | } |
||
| 108 | |||
| 109 | manager.list = req.app.get('projectFiles') |
||
| 110 | manager.editConfig = req.app.get('config') |
||
| 111 | manager.config = JSON.stringify(config) |
||
| 112 | |||
| 113 | var _hasBlock = (obj) ? obj.hasBlock : false |
||
| 114 | var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false |
||
| 115 | var _template = (filePath) ? '/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false |
||
| 116 | var _form = (obj) ? obj.form : false |
||
| 117 | var _json = (obj) ? obj.json : false |
||
| 118 | var _text = (obj) ? obj.text : false |
||
| 119 | // var _file = (tplUrl) ? tplUrl.draft.file : false |
||
| 120 | var _filePath = (req.query.filePath) ? req.query.filePath : false |
||
| 121 | if (_filePath) { |
||
| 122 | _filePath = '/' + _filePath.replace(/^\/+/, '') |
||
| 123 | } |
||
| 124 | |||
| 125 | var pageHtml = '' |
||
| 126 | if(typeof _json !== 'undefined' && _json !== null |
||
| 127 | && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) { |
||
| 128 | var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) |
||
| 129 | var page = new Page(_json.abe_meta.template, text, _json, false) |
||
| 130 | pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>') |
||
| 131 | } |
||
| 132 | |||
| 133 | var EditorVariables = { |
||
| 134 | pageHtml: pageHtml, |
||
| 135 | isHome: isHome, |
||
| 136 | abeUrl: '/abe/', |
||
| 137 | test: JSON.stringify(locale), |
||
| 138 | text: locale, |
||
| 139 | templatePath: req.params[0], |
||
| 140 | template: _template, |
||
| 141 | hasSingleBlock: _hasSingleBlock, |
||
| 142 | hasBlock: _hasBlock, |
||
| 143 | form: _form, |
||
| 144 | urlToSaveFile: _filePath, |
||
| 145 | // tplName: _file, |
||
| 146 | json: _json, |
||
| 147 | config: config, |
||
| 148 | Locales: coreUtils.locales.instance.i18n, |
||
| 149 | manager: manager, |
||
| 150 | express: { |
||
| 151 | res: res, |
||
| 152 | req: req |
||
| 153 | }, |
||
| 154 | abeVersion: pkg.version, |
||
| 155 | nonce: '\'nonce-' + res.locals.nonce + '\'' |
||
| 156 | } |
||
| 157 | EditorVariables = Hooks.instance.trigger('afterVariables', EditorVariables) |
||
| 158 | |||
| 159 | if (debugJson) { |
||
| 160 | var dj = _json |
||
| 161 | if(debugJsonKey && typeof dj[debugJsonKey] !== 'undefined' && dj[debugJsonKey] !== null) { |
||
| 162 | dj = dj[debugJsonKey] |
||
| 163 | } |
||
| 164 | res.set('Content-Type', 'application/json') |
||
| 165 | res.send(JSON.stringify(dj)) |
||
| 166 | }else if (debugHtml) { |
||
| 167 | res.set('Content-Type', 'text/plain') |
||
| 168 | res.send(_text) |
||
| 169 | }else { |
||
| 170 | res.render(config.abeEngine, EditorVariables) |
||
| 171 | } |
||
| 172 | }).catch((e) => { |
||
| 173 | console.log('error', e) |
||
| 174 | }) |
||
| 175 | } |
||
| 176 | |||
| 177 | export default route |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.